// pages/goods_detail/index.js
import { request } from "../../request/index.js";
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods_id: '',
    detailList: [],
    //是否被收藏
    iscollect: false
  },

  //记录商品
  goodsInfo: [],

  //点击收藏按钮
  collectGoods () {
    let iscollect = false;
    let collect = wx.getStorageSync("collect") || [];
    let Index = collect.findIndex(v => v.goods_id === this.goodsInfo.goods_id)
    if (Index === -1) {
      //没有收藏过
      collect.push(this.goodsInfo);
      iscollect = true
      wx.showToast({
        title: '收藏成功',
        icon: 'success',
        mask: true
      })
    } else {
      collect.splice(Index, 1);
      iscollect = false;
      wx.showToast({
        title: '取消成功',
        icon: 'success',
        mask: true
      })
    }
    wx.setStorageSync("collect", collect);

    this.setData({
      iscollect
    })
  },
  //点击放大图片

  handlePrevewImage (e) {
    const urls = this.goodsInfo.pics.map(v => v.pics_mid);
    const index = e.currentTarget.dataset.url;
    wx - wx.previewImage({
      current: index,
      urls: urls
    })
  },
  //加入购物车
  addCar () {

    let cart = wx.getStorageSync("cart") || [];

    let Index = cart.findIndex(v => v.goods_id === this.goodsInfo.goods_id)
    console.log(Index)
    if (Index === -1) {
      // 不存在 第一次添加
      this.goodsInfo.num = 1;
      this.goodsInfo.checked = true;
      cart.push(this.goodsInfo);
    } else {
      cart[Index].num++;
    }
    //重新添加缓存
    wx.setStorageSync("cart", cart);
    //弹窗提示
    wx.showToast({
      title: '加入成功',
      mask: true
    });

  },
  /**
 * 生命周期函数--监听页面加载
 */
  onLoad: function (options) {
    // console.log(options.goods_id)

  },
  async getGoodsDetail (goods_id) {
    const res = await request({ url: "/goods/detail", data: { goods_id } })
    // console.log(res)
    this.goodsInfo = res
    // 获取缓从中的收藏数组
    let collect = wx.getStorageSync("collect") || [];
    //判断当前商品是否被收藏
    let iscollect = collect.some(v => v.goods_id === this.goodsInfo.goods_id);
    this.setData({
      detailList: {
        goods_name: res.goods_name,
        goods_price: res.goods_price,
        goods_introduce: res.goods_introduce,
        pics: res.pics
      },
      iscollect
    })

    // console.log(res.pics)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // console.log(this.goods_id)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.goods_id = options.goods_id
    let pages = getCurrentPages();
    let currentPages = pages[pages.length - 1];
    const { goods_id } = currentPages.options;
    this.getGoodsDetail(goods_id)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})